"""Controller for current groups."""

from mnemoply.controllers.lookup import TableLookupDataSource, LookupControllerWithFilter
from mnemoply.controllers.Group import GroupController
from turbogears import identity, expose
from mnemoply.widgets import PlanField
import cherrypy
from mnemoply.utils import Wrapper
from mnemoply.model.auth import AuthUserOffice, AuthUserFaculty, AuthUserOrg

#class CurrentGroupDataSource(TableLookupDataSource):
#    """
#        Provides `group_lookup` with filtration by `is_open`.
#    """
#
#    def __init__(self):
#        TableLookupDataSource.__init__(self, 'group_lookup')
#
#    def _buildWhereChunks(self, request):
#        chunks = TableLookupDataSource._buildWhereChunks(self, request)
#        chunks.append("(is_open)")  # see schema-addon/views.sql
#        chunks.append(self.buildChunksOr("office", AuthUserOffice))
#        chunks.append(self.buildChunksOr("faculty", AuthUserFaculty))
#        chunks.append(self.buildChunksOr("organization", AuthUserOrg))
#        return chunks
 
class GroupWithFilterController(LookupControllerWithFilter):
    """Controller for current groups."""
    filter_fields = [PlanField(name="XXX")]
    @expose()
    def show():
        self.filters.append(dict(id=1))
        return self.list()
    
    def prepare_filter(self, **kw):
        args = LookupControllerWithFilter.prepare_filter(self, **kw)
        plan = "-1"
        if kw.has_key("XXX") and kw["XXX"]["plan"] :
            plan = kw["XXX"]["plan"]
        if plan!="-1" or args.has_key("flt_plan_id"):
            args["flt_plan_id"] = "="+plan
        return args
    @expose()
    def list(self):
        self.default_filters = {"plan_id":"=-1"}
        return LookupControllerWithFilter.list(self)

#exposed = GroupWithFilterController()